import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class LC_18 {
    public List<List<Integer>> fourSum(int[] nums, int target) {
        Arrays.sort(nums);
        List<List<Integer>> result = new ArrayList<>();
        if(nums.length < 4)return result;
        for(int k = 0; k < nums.length; k++){
            if(nums[k] >= 0 && nums[k] > target){
                return result;
            }
            if(k > 0 && nums[k] == nums[k - 1]){
                continue;
            }
            for(int i = k+1; i < nums.length; i++){

                if(i > k+1 && nums[i] == nums[i - 1]){
                    continue;
                }
                int left = i + 1;
                int right = nums.length - 1;
                while(left < right){
                    if(nums[k] + nums[i] + nums[left] + nums[right] > target){
                        right--;
                    }else if(nums[k] + nums[i] + nums[left] + nums[right] < target){
                        left++;
                    }else{
                        result.add(Arrays.asList(nums[k] , nums[i] , nums[left] , nums[right]));
                        while(left < right && nums[left] == nums[left+1]){left++;}
                        while(left < right && nums[right] == nums[right-1]){right--;}
                        left++;
                        right--;
                    }
                }

            }
        }
        return result;
    }
}
